// pages/apply-refund/apply-refund.js
var api = require('../../config/api.js');
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgList: [],
    showReason: false,
    strReason: '',
    reasonList: [{
        id: 1,
        name: '退运费',
        check: false
      },
      {
        id: 2,
        name: '商品信息描述不符',
        check: false
      },
      {
        id: 3,
        name: '质量问题',
        check: false
      },
      {
        id: 4,
        name: '物流慢',
        check: false
      },
      {
        id: 5,
        name: '其他',
        check: false
      }
    ],
    strReason: '请选择',
    name: '请选择',
    mColor: '#999999'
  },

  //获取数据
  getData() {
    let that = this;
    util.request(api.OrderDetails, {
      orderId: that.data.orderId
    }).then(function (res) {
      if (res.errno == 0) {
        that.setData({
          list: res.data.orderGoods,
          bean: res.data.orderInfo,
          picUrl: util.isEmpy(res.data.picUrl) ? 'https://zebra-1258707023.cos.ap-chengdu.myqcloud.com/yndwd/card_bg.png' : res.data.picUrl
        })
      } else {
        util.myToast(res.errmsg)
      }
    });
  },
  //申请退款
  getSubmitData() {
    let that = this;
    util.request(api.OrderRefund, {
      orderId: that.data.orderId,
      refundReason: that.data.strReason,
      refundPic: that.data.imgList
    }, 'post').then(function (res) {
      if (res.errno == 0) {
        wx.redirectTo({
          url: '/pages/my-refund/my-refund',
        })
      } else {
        util.myToast(res.errmsg)
      }
    });
  },
  //调用摄像头
  onOpenImg(e) {
    let that = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'], //可选择原图或压缩后的图片
      sourceType: ['album', 'camera'], //可选择性开放访问相册、相机
      success: function (res) {
        var tempFilePaths = res.tempFilePaths[0];

        util.uploadfile(api.Upload, tempFilePaths, 'file', {}).then(function (res) {
          var jsonObj = JSON.parse(res);
          if (jsonObj.errno == 0) {
            var imgList = that.data.imgList;
            imgList.push(jsonObj.data.url);
            that.setData({
              imgList: imgList
            })
          } else {
            wx.showToast({
              title: jsonObj.errmsg,
              icon: 'none',
            })
          }

        })
      },
    })
  },
  //获取输入框文字
  getReason(e) {
    this.setData({
      strReason: e.detail.value
    })
  },
  //提交申请
  onSubmit(e) {

    if (util.isEmpy(this.data.strReason)) {
      util.myToast('请输入退款说明')
    } else {
      if (this.data.bean.orderStatusText == '已发货' && this.data.imgList.length == 0) {
        //需要上传凭证
        util.myToast('至少上传一张凭证')
      } else {
        this.getSubmitData();
      }
    }



  },
  //选择退款原因
  onChooseReason(e) {
    this.setData({
      showReason: true
    })
  },
  //关闭窗口
  onClose(e) {
    this.setData({
      showReason: false
    })
  },
  //点击选择原因
  onClickReason(e) {
    var index = e.currentTarget.dataset.index;
    var id = e.currentTarget.dataset.id;
    var name = e.currentTarget.dataset.name;
    console.log("name--", name)
    var list = this.data.reasonList;
    for (var x in list) {
      list[x].check = false
    }
    if (list[index].id == id) {
      list[index].check = true
    }

    this.setData({
      reasonList: list,
      name: name
    })
  },
  //点击弹窗确认
  onSure(e) {
    if (this.data.name == '请选择') {
      wx.showToast({
        title: '请选择退款原因',
        icon: 'none'
      })
    } else {
      this.setData({
        strReason: this.data.name,
        showReason: false,
        mColor: '#333333'
      })
    }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var orderId = options.orderid;
    this.setData({
      orderId: orderId
    })
    this.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})